package org.dromara.system.controller;

import java.util.List;

import lombok.extern.slf4j.Slf4j;
import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.web.core.BaseController;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.system.domain.vo.SysAccountVo;
import org.dromara.system.domain.bo.SysAccountBo;
import org.dromara.system.service.ISysAccountService;
import org.dromara.common.mybatis.core.page.TableDataInfo;

/**
 * 账号
 *
 * @author yilong.fu
 * @date 2024-09-14
 */
@Validated
@RequiredArgsConstructor
@RestController
@Slf4j
@RequestMapping("/system/account")
public class SysAccountController extends BaseController {

    private final ISysAccountService sysAccountService;

    /**
     * 查询账号列表
     */
    @SaCheckPermission("system:account:list")
    @GetMapping("/list")
    public TableDataInfo<SysAccountVo> list(SysAccountBo bo, PageQuery pageQuery) {
        return sysAccountService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出账号列表
     */
    @SaCheckPermission("system:account:export")
    @Log(title = "账号", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(SysAccountBo bo, HttpServletResponse response) {
        List<SysAccountVo> list = sysAccountService.queryList(bo);
        ExcelUtil.exportExcel(list, "账号", SysAccountVo.class, response);
    }

    /**
     * 获取账号详细信息
     *
     * @param accountId 主键
     */
    @SaCheckPermission("system:account:query")
    @GetMapping("/{accountId}")
    public R<SysAccountVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long accountId) {
        return R.ok(sysAccountService.queryById(accountId));
    }

    /**
     * 新增账号
     */
    @SaCheckPermission("system:account:add")
    @Log(title = "账号", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody SysAccountBo bo) {
        return toAjax(sysAccountService.insertByBo(bo));
    }

    /**
     * 修改账号
     */
    @SaCheckPermission("system:account:edit")
    @Log(title = "账号", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody SysAccountBo bo) {
        return toAjax(sysAccountService.updateByBo(bo));
    }

    /**
     * 删除账号
     *
     * @param accountIds 主键串
     */
    @SaCheckPermission("system:account:remove")
    @Log(title = "账号", businessType = BusinessType.DELETE)
    @DeleteMapping("/{accountIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] accountIds) {
        return toAjax(sysAccountService.deleteWithValidByIds(List.of(accountIds), true));
    }
}
